package com.dazhao.animejaphome.home;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.dazhao.animejaphome.R;
import com.dazhao.animejaphome.bean.TestBean;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Jay on 2015/8/28 0028.
 */
public class MyFragment3Test extends Fragment {

    @BindView(R.id.text_question)
    TextView textViewQuestion;
    @BindView(R.id.bt_a)
    Button buttonA;
    @BindView(R.id.bt_b)
    Button buttonB;
    @BindView(R.id.bt_c)
    Button buttonC;
    @BindView(R.id.bt_d)
    Button buttonD;
    @BindView(R.id.bt_r)
    Button buttonR;

    String[] listFile;
    int count=0;


    @BindView(R.id.img_a)ImageView imgA;
    @BindView(R.id.img_b)ImageView imgB;
    @BindView(R.id.img_c)ImageView imgC;
    @BindView(R.id.img_d)ImageView imgD;

    public MyFragment3Test() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_test,container,false);
        ButterKnife.bind(this,view);

        buttonA.setTag("A");
        buttonB.setTag("B");
        buttonC.setTag("C");
        buttonD.setTag("D");

        try{
            listFile=MyFragment3Test.this.getContext().getAssets().list("testjson");
        }catch (IOException e){
            e.printStackTrace();
        }

        cleanRightOrWrong();
        if(count>=listFile.length){
            Snackbar.make(container,"目前只有"+listFile.length+"个资源，资源到头将会循环加载",Snackbar.LENGTH_SHORT).show();
            count=0;
        }
        loadQuestion(listFile[count]);
        count++;


        buttonR.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cleanRightOrWrong();
                if(count>=listFile.length){
                    Snackbar.make(view,"目前只有"+listFile.length+"个资源，资源到头将会循环加载",Snackbar.LENGTH_SHORT).show();
                    count=0;
                }

                loadQuestion(listFile[count]);
                count++;
            }
        });


        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        count=0;
        cleanRightOrWrong();
    }

    @Override
    public void onPause() {
        super.onPause();
        count=0;
        cleanRightOrWrong();
    }

    private void loadQuestion(String path){
        try{
            InputStream is=getContext().getAssets().open("testjson/"+path);
            InputStreamReader isReader=new InputStreamReader(is);
            Gson gson=new Gson();
            TestBean jsonBean = gson.fromJson(isReader,TestBean.class);

            String question =jsonBean.getQuestion();
            String a =jsonBean.getA();
            String b =jsonBean.getB();
            String c =jsonBean.getC();
            String d =jsonBean.getD();
            final String r =jsonBean.getR();

            textViewQuestion.setText(question);
            buttonA.setText(a);
            buttonB.setText(b);
            buttonC.setText(c);
            buttonD.setText(d);

            View.OnClickListener listener=new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String tag=view.getTag().toString();
                    cleanRightOrWrong();
                    if(r.equals(tag)){
                        ImageView rightView=getImageView(r);
                        Picasso.with(getContext()).load(R.drawable.answer_right).into(rightView);

//                        Toast.makeText(getContext(), "you were right", Toast.LENGTH_SHORT).show();
                    }else {
                        ImageView originalView =getImageView(tag);
                        Picasso.with(getContext()).load(R.drawable.answer_wrong).into(originalView);

//                        Toast.makeText(getContext(), "you were wrong", Toast.LENGTH_SHORT).show();
                    }
                }
            };
            buttonA.setOnClickListener(listener);
            buttonB.setOnClickListener(listener);
            buttonC.setOnClickListener(listener);
            buttonD.setOnClickListener(listener);

        }catch (IOException e){
            e.printStackTrace();
        }

    }
    private ImageView getImageView(String tag){
        switch (tag){
            case "A":return imgA;
            case "B":return imgB;
            case "C":return imgC;
            case "D":return imgD;

            default:return imgA;
        }
    }
    private void cleanRightOrWrong(){
        imgA.setImageDrawable(null);
        imgB.setImageDrawable(null);
        imgC.setImageDrawable(null);
        imgD.setImageDrawable(null);
    }

}
